$(function(){
    var header = new Header();
    header.init().init();
});



function Header(){
    this.init = function(){
        return HEADER;
    };
    
    var HEADER = {
        init : function(){
            HEADER.listeners.bindEvents();
            HEADER.funs.initHeaderSelect();
        //alert(WB2.checkLogin());
        },
        listeners : {
            bindEvents : function(){
                $.util.foreachFunction(this, [ "bindEvents" ]);
            },
            //页头链接
            bindHaderLink : function(){
                $('#header-nav a.a_header_link').click(function(){
                    $.util.initHiddenFormSubmit($('#frm_hidden_app_header'), SYSTEM_DATA.contextPath + '/Index/appTypeList.shtml', [{
                        'name' : 'type',
                        'value' : $(this).text()
                    }]);
                });
                
                $('#a_index_header_news').click(function(){
                    $.util.initHiddenFormSubmit($('#frm_hidden_app_header'), SYSTEM_DATA.contextPath + '/Index/newCenter.shtml', []);
                });
            },
            bindSearchText : function(){
                var text = '白泡泡';
                $("#txt_index_search").focus(function(){
                    var $this = $(this);
                    if(text == $this.val()){
                        $this.val('');
                    }
                }).blur(function(){
                    var $this = $(this);
                    if('' == $.trim($this.val())){
                        $this.val(text);
                    }
                });
            },
            bindSearch : function(){
                $("#txt_index_search").autocomplete({
                    source: function( request, response ) {
                        $.ajax({
                            url: SYSTEM_DATA.contextPath + "/Index/ajaxSearch.shtml",
                            dataType: "json",
                            data: {
                                'name': request.term
                            },
                            success: function( data ) {
                                response( $.map( data.data, function( item ) {
                                    return {
                                        label: item.name,
                                        value: item.name
                                    }
                                }));
                            }
                        });
                    },
                    minLength: 2
                });
            },
            bindSearchSubmit : function(){
                var text = '白泡泡';
                $("#btn_index_search_submit").click(function(){
                    var searchText = $.trim($("#txt_index_search").val());
                    if('' == searchText || text == searchText){
                        return;
                    }
                    
                    $.util.initHiddenFormSubmit($('#frm_hidden_app_header'), SYSTEM_DATA.contextPath + '/Index/search.shtml', [{
                        'name' : 'name', 
                        'value' : searchText
                    }]);
                });
            },
            bindWeiboLogin : function(){
                $("#a_weibo_sina").click(function(){
                    $.util.initHiddenFormSubmit($('#frm_hidden_app_header'), SYSTEM_DATA.contextPath + '/Index/weiboLogin/weibo/sina.shtml', []);
                });
                
                $("#a_weibo_tencent").click(function(){
                    $.util.initHiddenFormSubmit($('#frm_hidden_app_header'), SYSTEM_DATA.contextPath + '/Index/weiboLogin/weibo/tencent.shtml', []);
                });
            }
        },
        funs : {
            initWeibo : function(){
                WB2.anyWhere(function(W){
                    W.widget.connectButton({
                        id: "a_weibo_sina",
                        callback : {
                            login:HEADER.funs.loginWeibo,
                            logout: HEADER.funs.destroyWeibo
                        }
                    });
                });
            },
            initHeaderSelect : function(){
                if('index' == SYSTEM_DATA.index_flag){
                    $('#header-nav li').removeClass('selected');
                    
                    $('#li_head_index').addClass('selected');
                }else if('限时免费' == SYSTEM_DATA.type){
                    $('#header-nav li').removeClass('selected');
                    
                    $('#li_head_limit_free').addClass('selected');
                }else if('原创评测' == SYSTEM_DATA.type){
                    $('#header-nav li').removeClass('selected');
                    
                    $('#li_head_evaluation').addClass('selected');
                }else if('专题' == SYSTEM_DATA.type){
                    $('#header-nav li').removeClass('selected');
                    
                    $('#li_head_subject').addClass('selected');
                }else if('排行榜' == SYSTEM_DATA.type){
                    $('#header-nav li').removeClass('selected');
                    
                    $('#li_head_top').addClass('selected');
                }else if('新闻' == SYSTEM_DATA.type){
                    $('#header-nav li').removeClass('selected');
                    
                    $('#li_head_new').addClass('selected');
                }else if('搜索' == SYSTEM_DATA.type){
                    $('#header-nav li').removeClass('selected');
                    
                    $('#li_head_search').addClass('selected');
                }else{
                    $('#header-nav li').removeClass('selected');
                }
            }
        }
    };
}
